/**
 * 
 */
package jvs.peer.xmpp;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import org.jivesoftware.smack.packet.Packet;

/**
 * @author qiangli
 *
 */
public class XmppUtil {

	/**
	 *
	 */
	private XmppUtil() {
	}

	public static boolean isLoopback(Packet p) {
		String from = p.getFrom();
		String to = p.getTo();
		if (from == null || to == null) {
			return false;
		}
		return from.equals(to);
	}

	public static String ip() {

		try {
			Enumeration<?> nis = NetworkInterface.getNetworkInterfaces();
			while (nis.hasMoreElements()) {
				NetworkInterface ni = (NetworkInterface) nis.nextElement();
				Enumeration<?> ias = ni.getInetAddresses();
				while (ias.hasMoreElements()) {
					InetAddress ia = ((InetAddress) ias.nextElement());
					if (ia.isLoopbackAddress()) {
						continue;
					}
					String addr = ia.getHostAddress();
					return addr;
				}
			}
		} catch (Exception e) {
			//
		}
		return null;
	}
}
